package com.junxian.springboot03.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

@Slf4j
public class CallableTest {

    public static void main(String[] args) {
        MyCallAble myCallAble = new MyCallAble();
        //FutureTask 实现了Future和Runnable接口，所以它的实例可以放在new Thread(futureTask).start();
        FutureTask futureTask = new FutureTask<>(myCallAble);//FutureTask的构造函数可以由Callable和Runnable作为参数
        new Thread(futureTask).start();//futureTask实现了Runnable接口，所以可以直接放进来启动线程
        try {
            Integer integer = (Integer)futureTask.get();
            System.out.println(integer);
        } catch (InterruptedException e) {
            log.error("InterruptedException failure:{}", e);
        } catch (ExecutionException e) {
            log.error("ExecutionException failure:{}", e);
        }
    }

    static class MyCallAble implements Callable<Integer>{
        @Override
        public Integer call() {
            System.out.println("Hello World!");
            return 123;
        }
    }

}
